<%@ Page Language="C#" MasterPageFile="~/FiledbookMaster.Master" AutoEventWireup="true" CodeBehind="AddFieldWork.aspx.cs" Inherits="Fieldbook.Testwebsite.User.AddFieldWork" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <script type="text/javascript">
        // javacsript when everything else fails
        var operations = new Array(
           <%= Fieldbook.Business.JSONSerializator<Fieldbook.Entities.Operation>.ListToJson(this.operations, "OperationID", "CategoryID", "Description") %>
        );
        
        var machines = new Array(
           <%= Fieldbook.Business.JSONSerializator<Fieldbook.Entities.Machine>.ListToJson(this.machines, "MachineID", "CategoryID", "Registration", "Description") %>
        );

        var resources = new Array(
           <%= Fieldbook.Business.JSONSerializator<Fieldbook.Entities.Resource>.ListToJson(this.resources, "ResourceID", "CategoryID", "Description", "MeasureunitID") %>
        );
                
        var fieldworkMU = new Array(
           <%= Fieldbook.Business.JSONSerializator<Fieldbook.Entities.Measureunit>.ListToJson(this.fieldworkMU, "Measureunitid", "Description") %>
        )        

        var allMU = new Array(
           <%= Fieldbook.Business.JSONSerializator<Fieldbook.Entities.Measureunit>.ListToJson(this.allMeasureUnits, "Measureunitid", "Description") %>
        )        
        
        var houseHoldFields = new Array(
           <%= Fieldbook.Business.JSONSerializator<Fieldbook.Entities.Field>.ListToJson(this.houseHoldFields, "Fieldid", "Cataster", "Area", "Measureunitid", "FieldStateID") %>
        ) 
        
        var operationCount = 0;
        var machineCount = 0;
        var resourceCount = 0;
        
        function AddOperationRow()
        {
            operationCount++;
            document.getElementById('operationCount').value = operationCount;
            var selObjOperations = document.getElementById('fieldOperatoinID');
            var operationID = selObjOperations.options[selObjOperations.selectedIndex].value;
            var operationName = selObjOperations.options[selObjOperations.selectedIndex].text;
            
            selObjOperations.remove(selObjOperations.selectedIndex);
            
            var table = document.getElementById('<%= this.operationsTable.ClientID %>');
            var lastRow = table.rows.length;
            var row = table.insertRow(lastRow);
            
            var cell = row.insertCell(0);
            var textNode = document.createTextNode(operationName);
            cell.appendChild(textNode);
            
            var el = document.createElement('input');
            el.type = 'hidden';
            el.name = 'hiddenOperationID' + operationCount;
            el.value = operationID;
            cell.appendChild(el);
            
            cell = row.insertCell(1);
            var el = document.createElement('input');
            el.type = 'text';
            el.name = 'txtTotalCost' + operationCount;
            el.className = "txtnumber";
            cell.appendChild(el);

            return false;
        }
        
        function AddMachineRow()
        {
            machineCount++;
            document.getElementById('machineCount').value = machineCount;
            var selObjMachines = document.getElementById('fieldMachineID');
            var machineID = selObjMachines.options[selObjMachines.selectedIndex].value;
            var machineName = selObjMachines.options[selObjMachines.selectedIndex].text;
            
            selObjMachines.remove(selObjMachines.selectedIndex);
            
            var table = document.getElementById('<%= this.machinesTable.ClientID %>');
            var lastRow = table.rows.length;
            var row = table.insertRow(lastRow);
            
            var cell = row.insertCell(0);
            var textNode = document.createTextNode(machineName);
            cell.appendChild(textNode);
            
            var el = document.createElement('input');
            el.type = 'hidden';
            el.name = 'hiddenMachineID' + machineCount;
            el.value = machineID;
            cell.appendChild(el);
            
            cell = row.insertCell(1);
            cell.className = "right";
            var sel = document.createElement('select');
            sel.name = 'machineMU' + machineCount;
            for (var i=0; i<fieldworkMU.length; i++)
            {
              sel.options[i] = new Option(fieldworkMU[i].Description, fieldworkMU[i].Measureunitid);  
            }
            cell.appendChild(sel);
            
            cell = row.insertCell(2);
            var el = document.createElement('input');
            el.type = 'text';
            el.name = 'txtMachineQuota' + machineCount;
            el.className = "txtnumber";
            cell.appendChild(el);

            return false;
        }        
        
        function AddResourceRow()
        {
            resourceCount++;
            document.getElementById('resourceCount').value = resourceCount;
            var selObjResources = document.getElementById('fieldResourceID');
            var resourceID = selObjResources.options[selObjResources.selectedIndex].value;
            var resourceName = selObjResources.options[selObjResources.selectedIndex].text;
            var measureUnitID = selObjResources.options[selObjResources.selectedIndex].getAttribute("measureUnitID");
            selObjResources.remove(selObjResources.selectedIndex);
            
            var table = document.getElementById('<%= this.resourcesTable.ClientID %>');
            var lastRow = table.rows.length;
            var row = table.insertRow(lastRow);
            
            var cell = row.insertCell(0);
            var textNode = document.createTextNode(resourceName);
            cell.appendChild(textNode);
            
            var el = document.createElement('input');
            el.type = 'hidden';
            el.name = 'hiddenResourceID' + resourceCount ;
            el.value = resourceID;
            cell.appendChild(el);
            
            cell = row.insertCell(1);
            for (var i=0; i<allMU.length; i++)
            {
              if (allMU[i].Measureunitid == measureUnitID)
              {
                textNode = document.createTextNode(allMU[i].Description);
                cell.appendChild(textNode);
                break;
              }
            }
            
            cell = row.insertCell(2);
            var el = document.createElement('input');
            el.type = 'text';
            el.name = 'txtResourceQuota' + resourceCount;
            el.className = "txtnumber";
            cell.appendChild(el);

            return false;
        } 
        
        function populateOperations()
        {
            var selObjCategoryID = document.getElementById('<%= this.selectOperationCategory.ClientID %>');
            var selectedCategory = selObjCategoryID.options[selObjCategoryID.selectedIndex].value;
            var selObjOperations = document.getElementById('fieldOperatoinID');
            selObjOperations.options.length=0;
            for(var i=0; i<this.operations.length; i++)
            {
                if (selectedCategory == operations[i].CategoryID)
                {
                    var option = document.createElement("OPTION");
                    option.text = operations[i].Description;
                    option.value = operations[i].OperationID;
                    selObjOperations.options.add(option);
                }
            }
        }
        
        function populateMachines()
        {
            var selObjCategoryID = document.getElementById('<%= this.selectMachineCategory.ClientID %>');
            var selectedCategory = selObjCategoryID.options[selObjCategoryID.selectedIndex].value;
            var selObjMachines = document.getElementById('fieldMachineID');
            selObjMachines.options.length=0;
            for(var i=0; i<this.machines.length; i++)
            {
                if (selectedCategory == machines[i].CategoryID)
                {
                    var option = document.createElement("OPTION");
                    option.text = machines[i].Registration + " (" + machines[i].Description + ")";
                    option.value = machines[i].MachineID;
                    selObjMachines.options.add(option);
                }
            }        
        }
        
        function populateResources()
        {
            var selObjCategoryID = document.getElementById('<%= this.selectResourceCategory.ClientID %>');
            var selectedCategory = selObjCategoryID.options[selObjCategoryID.selectedIndex].value;
            var selObjResources = document.getElementById('fieldResourceID');
            selObjResources.options.length=0;
            for(var i=0; i<this.resources.length; i++)
            {
                if (selectedCategory == resources[i].CategoryID)
                {
                    var option = document.createElement("OPTION");
                    option.text = resources[i].Description;
                    option.value = resources[i].ResourceID;
                    option.setAttribute("measureUnitID", resources[i].MeasureunitID);
                    selObjResources.options.add(option);
                }
            }        
        }
        
        function poupulateFields()
        {
            var selObjField = document.getElementById('selectField');
            for(var i=0; i<this.houseHoldFields.length; i++)
            {
                var option = document.createElement("OPTION");
                option.value = houseHoldFields[i].Fieldid;
                option.text = houseHoldFields[i].Cataster + " (" + houseHoldFields[i].Area + ")";
                selObjField.options.add(option);
            }
            showFiledStatus(selObjField);
        }        
        
        function showFiledStatus(selObjField)
        {
            var selObjFieldState = document.getElementById('<%= this.ddlFieldState.ClientID %>');            
            var fieldStatusID = houseHoldFields[selObjField.selectedIndex].FieldStateID;
            for (var i=0; i<selObjFieldState.options.length; i++)
            {
                if (selObjFieldState.options[i].value == fieldStatusID)
                {
                    selObjFieldState.selectedIndex = i;
                    return;
                }
            }
        }
        
    </script>
    <input type="hidden" id="operationCount" name="operationCount" />
    <input type="hidden" id="machineCount" name="machineCount" />
    <input type="hidden" id="resourceCount" name="resourceCount" />
    
    <asp:Label ID="lblOperationStatus" runat="server" EnableViewState="false"/>
    
    <div style="width:850px;">
        Operations
        <hr />
        <div style="float:left; width:420px;">
        <table>
            <tr>
                <td>
                    Operation category
                </td>
                <td>
                    <select id="selectOperationCategory" runat="server"
                        datasourceid="OperationCategoriesODS"
                        datavaluefield="CategoryID"
                        datatextfield="Description"
                        onchange="populateOperations();">
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    Operation
                </td>
                <td >
                    <select id="fieldOperatoinID"/>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <a href="#" onclick="return AddOperationRow();">Add</a>
                </td>
            </tr>
        </table>
        </div>
        <div style="float:right; width:420px;">
            <asp:Table id="operationsTable" runat="server">
                <asp:TableHeaderRow>
                    <asp:TableHeaderCell Text="Name" />
                    <asp:TableHeaderCell Text="TotalCost" />
                </asp:TableHeaderRow>
            </asp:Table>
        </div>
    </div>
    <br />
    <br />
    <div style="width:850px;">
        Machines
        <hr />
        <div style="float:left; width:420px;">
        <table>
            <tr>
                <td>
                    Machine category
                </td>
                <td>
                    <select id="selectMachineCategory" runat="server"
                        datasourceid="MachneCategoriesODS"
                        datavaluefield="CategoryID"
                        datatextfield="Description"
                        onchange="populateMachines();">
                    </select>                    
                </td>
            </tr>
            <tr>
                <td>
                    Machine
                </td>
                <td>
                    <select id="fieldMachineID"/>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <a href="#" onclick="return AddMachineRow();">Add</a>
                </td>
            </tr>
        </table>
        </div>
        <div style="float:right; width:420px;">
            <asp:Table ID="machinesTable" runat="server">
                <asp:TableHeaderRow>
                    <asp:TableHeaderCell Text="Machine" />
                    <asp:TableHeaderCell Text="MeasureUnit" />
                    <asp:TableHeaderCell Text="Quota" />
                </asp:TableHeaderRow>
            </asp:Table>
        </div>
    </div>    
    <br />
    <br />
    <div style="width:850px;">
        Resources
        <hr />
        <div style="float:left; width:420px;">
            <table>
                <tr>
                    <td>
                        Resource category
                    </td>
                    <td>
                        <select id="selectResourceCategory" runat="server"
                        datasourceid="ResourceCategoriesODS"
                        datavaluefield="CategoryID"
                        datatextfield="Description"
                        onchange="populateResources();"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        Resource
                    </td>
                    <td>
                        <select id="fieldResourceID"/>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <a href="#" onclick="return AddResourceRow();">Add</a>
                    </td>
                </tr>                
            </table>        
        </div>
        <div style="float:right; width:420px;">
            <asp:Table ID="resourcesTable" runat="server">
                <asp:TableHeaderRow>
                    <asp:TableHeaderCell Text="Resource" />
                    <asp:TableHeaderCell Text="MeasureUnit" />
                    <asp:TableHeaderCell Text="Quota" />
                </asp:TableHeaderRow>
            </asp:Table>        
        </div>
    </div> 
    <br />
    <br />
    <div style="width:850px;">
        General info
        <hr />
        <div style="float:left; width:420px;">
            <table>
                <tr>
                    <td>
                         Field
                    </td>
                    <td>
                        <select id="selectField" name="selectField" onchange="showFiledStatus(this);"/>
                    </td>
                </tr>            
                <tr>
                    <td>
                         Executor
                    </td>
                    <td>
                        <asp:DropDownList ID="ddlExecutorID" runat="server">
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td>
                        Date
                    </td>
                    <td>
                    <asp:TextBox ID="txtDate" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td>
                         Field state
                    </td>
                    <td>
                        <asp:DropDownList ID="ddlFieldState" runat="server">
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td>
                        Total cost
                    </td>
                    <td>
                        <asp:TextBox id="txtTotalCost" runat="server" CssClass="txtnumber" />
                    </td>
                </tr>
             </table>
         </div>
         <div style="float:right; width:420px;">
             <table>

                <tr>
                    <td>
                        Remarks
                    </td>
                    <td>
                        <asp:TextBox ID="txtRemarks" runat="server" TextMode="MultiLine" Rows="8" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div style="width: 850px;">
        <hr />
        <asp:LinkButton ID="linkSave" OnCommand="linkAddFieldWork_Command" runat="Server" Text="Save" />
    </div>
    <asp:ObjectDataSource ID="FieldsODS" runat="server" TypeName="Fieldbook.Business.FieldController"
        SelectMethod="LoadHouseHoldFields">
        <SelectParameters>
            <asp:SessionParameter Name="userID" SessionField="userID" Type="int64" />
        </SelectParameters>
    </asp:ObjectDataSource>    
    <asp:ObjectDataSource ID="OperationCategoriesODS" runat="server" TypeName="Fieldbook.Business.OperationController" SelectMethod="LoadOperationCategories">
        <SelectParameters>
            <asp:SessionParameter Name="userID" Type="Int64" SessionField="userID" />
        </SelectParameters>
    </asp:ObjectDataSource>    
    <asp:ObjectDataSource ID="MachneCategoriesODS" runat="server" TypeName="Fieldbook.Business.MachineController" SelectMethod="LoadMachineCategories">
        <SelectParameters>
            <asp:SessionParameter Name="userID" Type="Int64" SessionField="userID" />
        </SelectParameters>
    </asp:ObjectDataSource>
    <asp:ObjectDataSource ID="ResourceCategoriesODS" runat="server" TypeName="Fieldbook.Business.ResourceController" SelectMethod="LoadResourceCategories">
        <SelectParameters>
            <asp:SessionParameter Name="userID" Type="Int64" SessionField="userID" />
        </SelectParameters>
    </asp:ObjectDataSource>
    
    <script type="text/javascript">
        populateOperations();
        populateMachines();
        populateResources();
        poupulateFields();
    </script>
    
</asp:Content>
